package com.qigao.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.AccountLogService;
import com.qigao.mall.commons.dto.AccountLogCondDto;
import com.qigao.mall.commons.dto.AccountLogResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.jdbc.mapper.TbAccountLogMapper;
import com.qigao.mall.jdbc.model.TbAccountLog;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName market-parent
 * @date 2020-06-2020-06-28 11:03
 * @Description:
 */
@Service
public class AccountLogServiceImpl implements AccountLogService {
    @Resource
    private TbAccountLogMapper tbAccountLogMapper;
    @Override
    public Result<PageInfo<AccountLogResultDto>> listPage(AccountLogCondDto accountLogCondDto) {
        PageHelper.startPage(accountLogCondDto.getPageNum(),accountLogCondDto.getPageSize());
        Page<TbAccountLog> tbAccountLogPage=(Page<TbAccountLog>)tbAccountLogMapper.selectByCond(accountLogCondDto);
        return convertToDto(tbAccountLogPage);
    }

    @Override
    public List<AccountLogResultDto> findOrderResultByCond(AccountLogCondDto accountLogCondDto) {
        List<TbAccountLog> tbAccountLogList=(List<TbAccountLog>)tbAccountLogMapper.selectByCond(accountLogCondDto);
        List<AccountLogResultDto> accountLogResultDtos=new ArrayList<>();
        for(TbAccountLog tbAccountLog:tbAccountLogList){
            AccountLogResultDto accountLogResultDto=new AccountLogResultDto();
            BeanUtils.copyProperties(tbAccountLog,accountLogResultDto);
            accountLogResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbAccountLog.getCreateTime()));
            accountLogResultDto.setModiTime(DateUtil.getDateTimeFormat(tbAccountLog.getModiTime()));
            accountLogResultDtos.add(accountLogResultDto);
        }
        return accountLogResultDtos;
    }

    private Result<PageInfo<AccountLogResultDto>> convertToDto(Page<TbAccountLog> accountLogPage) {
        Page<AccountLogResultDto> accountLogResultDtoPage=new Page<AccountLogResultDto>();
        BeanUtils.copyProperties(accountLogPage,accountLogResultDtoPage);
        if(accountLogPage.getResult()!=null){
            for(TbAccountLog tbAccountLog:accountLogPage.getResult()){
                AccountLogResultDto accountLogResultDto=new AccountLogResultDto();
                BeanUtils.copyProperties(tbAccountLog,accountLogResultDto);
                accountLogResultDto.setCreateTime(DateUtil.getDateTimeFormat(tbAccountLog.getCreateTime()));
                accountLogResultDto.setModiTime(DateUtil.getDateTimeFormat(tbAccountLog.getModiTime()));
                accountLogResultDtoPage.add(accountLogResultDto);
            }
        }
        return Result.newSuccess(new PageInfo<>(accountLogResultDtoPage));
    }
}
